<?php
require_once('TVB/PWORKS/mvc/IFilter.iface.php');
require_once('TVB/PWORKS/mvc/IAction.iface.php');
require_once('TVB/PWORKS/message/Message.class.php');
require_once('TVB/Auth/Auth.php');

class CommonFilter implements IFilter {

        public function before(IAction &$action) {
                try {
                        $data = AC::getDataByList();
                        $userId = $this->checkAuth($action);
                        $info = array('time'=>date('Y-m-d H:i'),'username'=>$userId);
                        $action->setData('info', $info);
                        switch($action->getConfig()->id) {
                            case 'premium_video_viewing':
                            case 'index':
                                $action->setData('month', null);
                                $action->setData('start_end_day', $data['start_end_day']);
                                $action->setData('dateby', $data['dateby']);
                                $action->setData('country', $data['country']);
                                $action->setData('age', $data['age']);
                                $action->setData('categorys', $data['categorys']);
                                $action->setData('gender', $data['gender']);
                                $action->setData('video_sortby', $data['video_sortby']);
                                $action->setData('video_subscription_plan', $data['video_subscription_plan']);
                                $action->setData('video_plan_package', $data['video_plan_package']);
                                $action->setData('custom_country',$data['custom_country']);
                                $action->setData('top_size',$data['top_size']);
                                break;
                            case 'video_bitrate':
                            case 'os_browser':
                                //$action->setData('bymonth',$data['bymonth']);
                                $action->setData('bymonth2',$data['bymonth2']);
                                $action->setData('custom_country',$data['custom_country']);
                                $action->setData('date_month_sortby', $data['date_month_sortby']);
                                $action->setData('dateby',$data['dateby']);
                                $action->setData('start_end_day',$data['start_end_day']);
                                break;
                            case 'plan_subscription':
                                $action->setData('age', $data['age']);
                                $action->setData('dateby', $data['dateby']);
                                $action->setData('start_end_day', $data['start_end_day']);
                                $action->setData('gender', $data['gender']);
                                $action->setData('custom_country',$data['custom_country']);
                                $action->setData('video_plan_package', $data['video_plan_package']);
                                $action->setData('date_month_sortby', $data['date_month_sortby']);
                                $action->setData('bymonth2',$data['bymonth2']);
                                $action->setData('bymonth',$data['bymonth']);
                                break;
                            case 'sign_up':
                                $action->setData('start_end_day', $data['start_end_day']);
                                $action->setData('dateby', $data['dateby']);
                                $action->setData('custom_country',$data['custom_country']);
                                $action->setData('date_month_sortby', $data['date_month_sortby']);
                                $action->setData('bymonth',$data['bymonth']);
                                $action->setData('bymonth2',$data['bymonth2']);
                                break;
                            case 'daily_summary':
                                $action->setData('daily_date',$data['daily_date']);
                                break;
                            case 'reg_active_confirm':
                            case 'mstat_login_out':
                            case 'mstat_forget_password':
                            case 'mstat_edit_profile':
                            case 'mstat_member':
                                $action->setData('member_showbydate',$data['member_showbydate']);
                                $action->setData('member_dateby',$data['member_dateby']);
                                break;
                            case 'hot_search':
                                $action->setData('member_showbydate',$data['member_showbydate']);
                                $action->setData('member_dateby',$data['member_dateby']);
                                $action->setData('member_top_limit',$data['member_top_limit']);
                                $action->setData('member_product',$data['member_product']);
                                break;
                        }

                }
                catch(Exception $e)
                {
                        return null;
                }

                return null;
        }

        public function after(IAction &$action) {

                return null;
        }



        private function checkAuth(IAction $action)
        {
                $userId = TVBGetAuthUserFromCookie();
                if ($userId == FALSE) {
                        $eUrl=Urlencode("http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
                        $url = "http://auth.tvb.com/login.php?destUrl={$eUrl}";
                        header("Location: $url");
                        exit;
                }else{
                        return $userId;
                }
        }

}